import {
  addOutFileApply,
  copyFileToUpload,
  copyOutFileToPrivate,
  deleteOutSideFileOrDir,
  getPreviewPathForOuter,
  moveFileToUpload,
  moveOutFileToPrivate,
  queryDatadictList,
  queryDirTreeList,
  queryOutFileList,
} from '@/services/workspace';

export default {
  namespace: 'outFileModel',

  state: {
    // 列表数据
    data: {
      canUpload:false,
      canMoveOrCopy:false,
      dataList: [],
      pageNum: 0,
      pageSize: 0,
      totalCount: 0,
    },
    //目录树结构
    dirTreeList: [],
  },

  effects: {
    * queryList({payload, callback}, {call, put}) {


      const response = yield call(queryOutFileList, payload);
      if (response && response.result == 'ok') {
        yield put({
          type: 'save',
          payload: response,
        });
      }
      if (callback) callback(response);

    },

    //查询目录组织树
    * queryDirTreeList({payload, callback}, {call, put}) {
      const response = yield call(queryDirTreeList, payload);
      if (response && response.result == 'ok') {

        yield put({
          type: 'saveDirTreeList',
          payload: response,
        });
      }
    },
    //移动文件
    * moveFile({payload, callback}, {call, put}) {
      const response = yield call(moveOutFileToPrivate, payload);
      if (callback) callback(response);
    },
    //删除文件
    * deleteOutSideFileOrDir({payload, callback}, {call, put}) {
      const response = yield call(deleteOutSideFileOrDir, payload);
      if (callback) callback(response);
    },
    //复制文件
    * copyFileToUpload({payload, callback}, {call, put}) {
      const response = yield call(copyFileToUpload, payload);
      if (callback) callback(response);
    },
    //移动文件
    * moveFileToUpload({payload, callback}, {call, put}) {
      const response = yield call(moveFileToUpload, payload);
      if (callback) callback(response);
    },
    * queryDatadictList({payload, callback}, {call, put}) {
      const response = yield call(queryDatadictList, payload);
      if (callback) callback(response);
    },

    //复制文件
    * copyFile({payload, callback}, {call, put}) {
      const response = yield call(copyOutFileToPrivate, payload);
      if (callback) callback(response);
    },
    //预览文件
    * getPreviewLinkPath({payload, callback}, {call, put}) {
      const response = yield call(getPreviewPathForOuter, payload);
      if (callback) callback(response);
    },

  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data,
      };
    },
    saveDirTreeList(state, action) {
      return {
        ...state,
        dirTreeList: action.payload.data,
      };
    },
  },
};
